package kgame5.kgu {
import kgame5.kutil.KEvent;
import kgame5.kutil.T;

import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;

/**
 * @author kangkai
 * 倒计时发生器，3:29
 * 这个只是数据，没有视图（这样才能最灵活），视图不在这里
 * 每秒更新一次
 */
public class CountdownClock extends EventDispatcher{
	public static const 
		EVENTTYPE_UPDATED:String="EVENTTYPE_UPDATED",
		EVENTTYPE_TIMEOVER:String="EVENTTYPE_TIMEOVER";
		
	private var timer:Timer;
	
	private var leftSec:int;
	private var showedSec:int;
	private var showedMin:int;
	
	public function CountdownClock(){
		timer=new Timer(1000);
	}
	public function Start(p_leftSec:int){
		leftSec=p_leftSec;
		
		timer.addEventListener(TimerEvent.TIMER, onTimer);
		timer.start();
	}
	private function onTimer(e:TimerEvent){
		leftSec--;
		
		notifyUpdated();
		
		//T.p("sec:"+showedSec,"min:"+showedMin);
		if(leftSec==0){
			Clear();
			notifyTimeOver();
		}
	}
	public function Clear(){
		T.p("CountdownClock.Clera");
		timer.removeEventListener(TimerEvent.TIMER, onTimer);
		timer.stop();
	}
	
	
	private function notifyTimeOver(){
		T.p("!!CountdownClock.notifyTimeOver");
		this.dispatchEvent(new KEvent(EVENTTYPE_TIMEOVER));
	}
	private function notifyUpdated(){
		this.dispatchEvent(new KEvent(EVENTTYPE_UPDATED));
	}
	
	
	
	
	public function getShowedSec():int{
		return leftSec%60;
	}
	public function getShowedMin():int{
		return leftSec/60;
	}
}//end of class
}
